package com.sy.simpleegg.http.callback;

import com.sy.logger.Logger;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public abstract class UploadLogCallback extends BaseCallback{


    @Override
    public void onFail(Call call, IOException e) {
        logException(e);
        fail(call, e);
    }

    @Override
    public void onResp(Call call, Response response) throws IOException {
        response(call, response);
    }

    protected abstract void fail(Call call, IOException e);

    protected abstract void response(Call call, Response response) throws IOException;

    /**
     * 打印异常信息
     * @param e
     */
    private void logException(Exception e){
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(e.getMessage()).append("\n");
            Throwable cause = e.getCause();
            while (cause != null){
                stringBuilder.append(cause.toString()).append("\n");
                for (StackTraceElement stackTraceElement : cause.getStackTrace()){
                    stringBuilder.append(stackTraceElement.toString()).append("\n");
                }
                cause = cause.getCause();
            }
            Logger.e("上传日志异常信息：" + e.getMessage());
        }catch (Exception e2){
            e2.printStackTrace();
        }
    }
}
